﻿using Halcon二次开发.Controller.Controller基类;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
using Halcon二次开发.View.九点标定;

namespace Halcon二次开发.Controller.九点标定功能
{
    public class NinePointCalibrationController
    {
        /// <summary>
        /// 点位个数
        /// </summary>
        public int pointNum;
        /// <summary>
        /// 矩阵路径
        /// </summary>
        public static string matrixPath;
        /// <summary>
        /// 控制变量
        /// </summary>
        HTuple Camera_x, Camera_y, Robot_x, Robot_y;
        HTuple HomMat2D;

        /// <summary>
        /// 输入变量，输出变量
        /// </summary>
        public double IntCamera_x;
        public double IntCamera_y;
        public double OutRobot_x;
        public double OutRobot_y;
        /// <summary>
        /// 窗体对象
        /// </summary>
        private NinePointCalibrationForm ninePointCalibrationForm;

        public NinePointCalibrationController(NinePointCalibrationForm ninePointCalibrationForm)
        {
            this.ninePointCalibrationForm = ninePointCalibrationForm;
             Camera_x = new HTuple();
             Camera_y = new HTuple();
             Robot_x = new HTuple();
             Robot_y = new HTuple();
        }
       /// <summary>
       /// 运行 将像素坐标转化为世界坐标
       /// </summary>
       /// <param name="Camera_x">像素坐标x</param>
       /// <param name="Camera_y">像素坐标y</param>
       /// <param name="robot_X">现实坐标</param>
       /// <param name="robot_Y">现实坐标</param>
        public void Run()
        {
            try
            {
                if (HomMat2D == null)
                {
                    throw new Exception("算子未矫正!");
                }
                HOperatorSet.AffineTransPoint2d(HomMat2D, IntCamera_x, IntCamera_y, out HTuple Robot_x, out HTuple Robot_y);
                //Robot_x.ToString();
                OutRobot_x = double.Parse(Robot_x.ToString());
                OutRobot_y = double.Parse(Robot_y.ToString());
            }
            catch(Exception ex)
            {
                string str=ex.Message.Contains("算子未矫正") ?ex.Message: "像素坐标转化失败！";
                throw new Exception(str);
            }
        }
        /// <summary>
        /// 矫正动作 生成矩阵
        /// </summary>
        public bool Correct()
        {

            try
             {
                if (pointNum>8)
                {
                    for (int i = 0; i < pointNum; i++)
                    {
                        Camera_x[i] = double.Parse(ninePointCalibrationForm.dataGridView1.Rows[i].Cells[0].Value.ToString());
                        Camera_y[i] = double.Parse(ninePointCalibrationForm.dataGridView1.Rows[i].Cells[1].Value.ToString());
                        Robot_x[i] = double.Parse(ninePointCalibrationForm.dataGridView1.Rows[i].Cells[2].Value.ToString());
                        Robot_y[i] = double.Parse(ninePointCalibrationForm.dataGridView1.Rows[i].Cells[3].Value.ToString());
                    }
                    HOperatorSet.VectorToHomMat2d(Camera_x, Camera_y, Robot_x, Robot_y, out HomMat2D);
                    return true;
                }
                else
                {
                    throw new Exception("矫正失败!");
                }
            }
            catch
            {
                throw new Exception("矫正失败!");
            }
        }
        /// <summary>
        /// 保存矩阵
        /// </summary>
        public bool SaveMatrix()
        {
            try
            {
                if (HomMat2D != null)
                {
                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    saveFileDialog.Title = "保存矩阵";
                    saveFileDialog.InitialDirectory = Application.StartupPath;
                    saveFileDialog.Filter = "矩阵文件|*.tup";
                    if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        matrixPath = saveFileDialog.FileName;
                        HOperatorSet.WriteTuple(HomMat2D, matrixPath);
                        return true;
                    }else
                    {
                       return  false;
                    }
                    
                }
                throw new Exception("算子未矫正!");
            }
            catch(Exception ex)
            {
                string str = ex.Message.Contains("算子未矫正") ? ex.Message : "保存失败！";
                throw new Exception(str);
            }
        }
        /// <summary>
        /// 打开矩阵
        /// </summary>
        public bool OpenMatrix()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择矩阵文件";
            openFileDialog.InitialDirectory = Application.StartupPath;
            openFileDialog.Filter = "矩阵文件|*.tup";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                NinePointCalibrationController.matrixPath = openFileDialog.FileName;
                HOperatorSet.ReadTuple(matrixPath, out HomMat2D);
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 新增节点
        /// </summary>
        public void AddPoint()
        {
            HomMat2D = null;
            DataGridViewRow newRow = new DataGridViewRow();
            newRow.CreateCells(ninePointCalibrationForm.dataGridView1);
            newRow.Cells[0].Value = 0;
            newRow.Cells[1].Value = 0;
            newRow.Cells[2].Value = 0;
            newRow.Cells[3].Value = 0;
            ninePointCalibrationForm.dataGridView1.Rows.Add(newRow);
            pointNum++;
            newRow.HeaderCell.Value = pointNum.ToString();
        }
        /// <summary>
        /// 删除节点
        /// </summary>
        public void DeletePoint()
        {
            if (pointNum > 0)
            {
                HomMat2D = null;
               pointNum--;
               ninePointCalibrationForm.dataGridView1.Rows.RemoveAt(pointNum);
            }
        }
        /// <summary>
        /// 写入像素坐标
        /// </summary>
        /// <param name="index"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void SetCameraPoint(int index, int x, int y)
        {
            ninePointCalibrationForm.dataGridView1.Rows[index].Cells[0].Value = x;
            ninePointCalibrationForm.dataGridView1.Rows[index].Cells[1].Value = y;
        }
        /// <summary>
        /// 写入实际坐标
        /// </summary>
        /// <param name="index"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void SetRobotPoint(int index, int x, int y)
        {
            ninePointCalibrationForm.dataGridView1.Rows[index].Cells[2].Value = x;
            ninePointCalibrationForm.dataGridView1.Rows[index].Cells[3].Value = y;
        }
    }

}
